package entity;

import common.Common;
import service.CallSerivce;
import service.SendService;

import java.util.Calendar;

public class TalkPackage extends ServicePackage implements CallSerivce, SendService {
    //话痨套餐
    private int talkTime;//通话时长（分钟）
    private int smsCount;//短信条数（条）

    public int getTalkTime() {
        return talkTime;
    }

    public void setTalkTime(int talkTime) {
        this.talkTime = talkTime;
    }

    public int getSmsCount() {
        return smsCount;
    }

    public void setSmsCount(int smsCount) {
        this.smsCount = smsCount;
    }
    public TalkPackage() {
        //套餐数据初始化
        this.talkTime = 500;
        this.smsCount = 30;
        this.price = 58.0;
    }

    public TalkPackage(int talkTime, int smsCount) {
        super();
        this.talkTime = talkTime;
        this.smsCount = smsCount;
    }

//显示套餐详情
    @Override
    public void showInfo() {
        System.out.println("话唠套餐：通话时长为" + this.talkTime + "分钟/月，短信条数为"
                + this.smsCount + "条/月，资费为" + this.price + "元/月。");
    }

    @Override
    public int call(int minCoun, MobileCard card) throws Exception {
        int temp=minCoun;
        for (int i = 0; i < minCoun; i++) {
            if (this.talkTime-card.getRealTalkTime()>=1){
                //第一种情况：套餐剩余通话时长可以付1分钟通话
                card.setRealTalkTime(card.getRealTalkTime()+1);//实际通话数据加1
            }else if (card.getMoney()>=0.2){
                //第二种情况：套餐通话时长已用完，账户余额可以支付1分钟通话，使用账户余额支付
                card.setRealTalkTime(card.getRealTalkTime()+1);//实际使用通话时长1分钟
                card.setMoney(Common.sub(card.getMoney(),0.2));//账户余额消费0.2元（1分钟额外通话）
                card.setConsumAmount(card.getConsumAmount()+0.2);
            }else {
                temp=i;
                throw new Exception("本次已通话"+i+"分钟，您的余额不足，请充值后在使用");
            }
        }
        return temp;
    }

    @Override
    public int sendMessage(int count, MobileCard card) throws Exception {
        int temp=smsCount;
        for (int i = 0; i < smsCount; i++) {
            if (this.smsCount-card.getRealSMSCount()>=1){
                //第一种情况：套餐剩余短信条数可以支付1条信息
                card.setRealSMSCount(card.getRealSMSCount()+1);//实际使用短信加1
            }else if (card.getMoney()>=0.1){
                //第二种情况：套餐短信条数已用完，账户余额可以支付1条短信，使用账户余额支付
                card.setRealSMSCount(card.getRealSMSCount()+1);//实际使用短信加1
                card.setMoney(Common.sub(card.getMoney(),0.1));//账户余额消费0.2元（1M短信费用）
                card.setConsumAmount(card.getConsumAmount()+0.2);
            }else {
                temp=i;
                throw new Exception("本次已发送短信"+i+"MB，您的余额不足，请充值后在使用");
            }
        }
        return temp;
    }
}
